package com.springboot.controller;

import com.springboot.dto.PatientDetails;
import com.springboot.model.OrderForm;
import com.springboot.model.PatientInfoForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;

/**
 * 本控制台用于医生查看预约详情
 * 点击该患者可以显示其预约详情
 */
@Controller

public class DoctorViewPatientDetailsController {
    @Autowired
    DoctorViewPatientService doctorViewPatientService;
    /**
     * 医生查看患者预约详情
     */
    @CrossOrigin
    @RequestMapping(value = "/getPatientMessage")
    public @ResponseBody
    String doctorViewPatientDetails(@RequestParam("doctorTel") String doctorTel,
                                    @RequestParam("patientTel") String patientTel,
                                    @RequestParam("bookDate") String bookDate) {
        System.out.println("医生查看患者预约详情");
        try {
            PatientDetails patientDetails=new PatientDetails();
            UserLoginForm doctor=doctorViewPatientService.findUserByTel(doctorTel);
            UserLoginForm patient=doctorViewPatientService.findUserByTel(patientTel);
            int doctorId=doctor.getUserId();
            int patientId=patient.getUserId();
            patientDetails.setPatientName(patient.getUserName());
            patientDetails.setLogoLink(patient.getLogoLink());
            patientDetails.setBookDate(bookDate);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
            OrderForm orderForm=new OrderForm();
            orderForm.setDoctorId(doctorId);
            orderForm.setPatientId(patientId);
            java.util.Date d = null;
            try {
                d = format.parse(bookDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            java.sql.Date date = new java.sql.Date(d.getTime());
            orderForm.setBootDate(date);
            //通过三个外键：医生id 患者id 预约日期查找到预约编号
            OrderForm order=doctorViewPatientService.findOrderById(orderForm);
            int bookId=order.getBootId();
            System.out.println(order.getBootId());
            PatientInfoForm patientInfoForm=doctorViewPatientService.findDetailsBybook(bookId);
            patientDetails.setSickSketch(patientInfoForm.getSickSketch());
            patientDetails.setSickInfo(patientInfoForm.getSickInfo());
            System.out.println(patientDetails.toString());
            return patientDetails.toString();
        } catch (Exception e) {
            return "False";
        }
    }
}
